<template>
  <div class="container">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="userName" required>
        <el-input
          v-model="ruleForm.userName"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password" required>
        <el-input
          type="password"
          v-model="ruleForm.password"
          autocomplete="off"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>

      <el-form-item label="确认密码" prop="checkPass" required>
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
          placeholder="请再次确认密码"
        ></el-input>
      </el-form-item>

      <el-form-item label="电话号码" prop="tel" required>
        <el-input
          v-model.number="ruleForm.tel"
          placeholder="请输入电话号码"
        ></el-input>
      </el-form-item>

      <el-form-item label="注册时间" required>
        <el-form-item prop="date">
          <el-date-picker
            type="datetime"
            placeholder="请选择日期"
            v-model="ruleForm.date"
            value-format="yyyy-MM-dd HH:mm:ss"
            style="width: 100%;"
          ></el-date-picker>
        </el-form-item>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          @click="submitForm('ruleForm')"
          style="width:100px"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')" style="width:100px"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { UserRegister } from "@/services/UserService";
export default {
  data() {
    //   用户名
    var validateUserName = (rule, value, callback) => {
      var reg = /^[\u4e00-\u9fa5]+$/;
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else if (!reg.test(value)) {
        callback(new Error("用户名只能输入中文"));
      } else {
        callback();
      }
    };
    // 密码
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    // 二次密码
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    // 电话号码
    var checkTel = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("电话号码不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("请输入数字值"));
        } else {
          if (value.toString().length != 11) {
            callback(new Error("电话号码有效位为11位"));
          } else {
            callback();
          }
        }
      }, 1000);
    };

    return {
      ruleForm: {
        userName: "",
        password: "",
        checkPass: "",
        tel: "",
        date: "",
      },
      rules: {
        userName: [
          { required: true, message: "用户名不能为空！", trigger: "blur" },
          { validator: validateUserName, trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          {
            validator: validatePass,
            message: "密码不能为空！",
            trigger: "blur",
          },
        ],
        checkPass: [
          {
            validator: validatePass2, trigger: "blur",
          },
        ],
        tel: [
          { validator: checkTel, trigger: "blur" }
        ],
        date: [
          { required: true, message: "日期不能为空！", trigger: "blur" },
          { message: "日期不能为空！", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$message('注册成功',2000);
          UserRegister(this.ruleForm).then((resp) => {});
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.container {
  margin-top: 20px;
  width: 30%;
}
</style>
